<?php
class Sales_order_model extends CI_Model
{
    private $table   = 'sales_order';
    private $table_2 = 'line_order_product';
    private $table_3 = 'line_base';
    private $table_4 = 'user';
    
    public function insertSalesOrder($postData)
    {
        $data = $postData;
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
   public function updatePayBank($pay_bank,$pay_from,$order_id)
    {
        $data = array(
                'pay_bank' => $pay_bank,
                'pay_from' => $pay_from
        );
        $this->db->where('order_id', $order_id);
        return $this->db->update($this->table, $data);
    }
    
    public function updateOrderContact($getData)
    {
        $data = array(
                'user_name' => $getData['user_name'],
                'telephone' => $getData['telephone'],
                'email'     => $getData['email'],
        );
        $this->db->where('order_id',$getData['order_id']);
        return $this->db->update($this->table,$data);
    }
    
    public function findByOrderId($order_id)
    {
        $this->db->select('sales_order.*');
        $this->db->from($this->table);
        $this->db->where('order_id', $order_id);
        return $this->db->get();
    }
    
    /**
     * 通过订单id查找订单信息。
     * @param unknown $order_id
     */
    public function findByOrderIdInfo($order_id)
    {
        $this->db->select('sales_order.*, line_order_product.line_name, line_order_product.start_date, line_order_product.adult_num, line_order_product.children_num,line_order_product.line_id');
        $this->db->from($this->table.' as sales_order');
        $this->db->join($this->table_2.' as line_order_product', 'line_order_product.order_id = sales_order.order_id');
        $this->db->where('sales_order.order_id', $order_id);
        return $this->db->get();
    }
    
    
    /**
     * 通过订单编号获取订单信息
     * @param unknown $order_sn
     */
    public function byOrderOrderSn($order_sn)
    {
        $this->db->select('sales_order.*, line_order_product.line_name, line_order_product.start_date, line_order_product.adult_num, line_order_product.children_num,line_order_product.line_id');
        $this->db->from($this->table.' as sales_order');
        $this->db->join($this->table_2.' as line_order_product', 'line_order_product.order_id = sales_order.order_id');
        $this->db->where('sales_order.order_sn', $order_sn);
        return $this->db->get();
    }
    
    /**
     * 付款成功修改订单状态
     * @param unknown $order_sn
     */
    public function payUpdateOrderStatus($order_sn)
    {
        $data = array(
            'order_state'  => 2, //已付款
            'order_status' => 4, //已付款
            'pay_time'     => date('Y-m-d H:i:s', time()),
            'updated_at'   => date('Y-m-d H:i:s', time())
        );
        $this->db->where('order_sn', $order_sn);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 修改当前订单状态
     * @param unknown $order_id
     * @param unknown $order_state
     * @param unknown $order_status
     */
    public function modifyOrderStatus($order_id, $order_state, $order_status)
    {
        $data = array(
            'order_state'  => $order_state, 
            'order_status' => $order_status, 
            'updated_at'   => date('Y-m-d H:i:s', time())
        );
        $this->db->where('order_id', $order_id);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 修改当前订单状态
     * @param unknown $order_id
     * @param unknown $order_status
     */
    public function updateOrderStatus($order_id, $order_status)
    {
        $data = array(
            'order_status' => $order_status,
            'updated_at'   => date('Y-m-d H:i:s', time())
        );
        $this->db->where('order_id', $order_id);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 确认完成修改订单状态
     * @param unknown $order_id
     */
    public function completeOrderStatus($order_id)
    {
        $data = array(
            'order_state'  => 2, //已付款
            'order_status' => 5, //已完成
            'updated_at'   => date('Y-m-d H:i:s', time())
        );
        $this->db->where('order_id', $order_id);
        return $this->db->update($this->table, $data);
    }
    
   
    /**
     * 分润时 - 更新当前需支付的价格。
     * @param unknown $orderPayTotal
     * @param unknown $order_id
     */
    public function updateOrderPayTotalSubtract($orderPayTotalSubtract, $order_id)
    {
        $this->db->set('order_pay_total', 'order_pay_total-'.$orderPayTotalSubtract, false);
        $this->db->where('order_id', $order_id);
        return $this->db->update($this->table);
    }
    
    /**
     * 议价时 - 更新当前需支付的价格。
     * @param unknown $orderPayTotal
     * @param unknown $order_id
     */
    public function updateOrderPayTotal($orderPayTotal, $order_id)
    {
        $data = array(
            'order_pay_total' => $orderPayTotal
        );
        $this->db->where('order_id', $order_id);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 找出在线支付，4个小时内未付款的，
     */
    public function findNotPayOrder()
    {
        $this->db->where('order_state', 1); //订单状态未支付的
        $this->db->where('order_status <=', 2);//订单状态未支付的
        $this->db->where('pay_style_type', 1); //支付类型属于“在线支付”的
        $this->db->where('updated_at <=', date('Y-m-d H:i:s', time()-4*60*60)); //4小时内未付款。
        return $this->db->get($this->table);
    }
    
    /**
     * 找出属于当前订单状态的订单。
     * @param unknown $order_state
     * @param unknown $order_status
     */
    public function findComplateOrderStatus($order_state, $order_status)
    {
        $this->db->select('sales_order.*, line_order_product.line_id, line_order_product.line_name, line_order_product.start_date, line_order_product.travel_days, line_order_product.adult_num, line_order_product.children_num');
        $this->db->from($this->table.' as sales_order');
        $this->db->join($this->table_2.' as line_order_product', 'line_order_product.order_id = sales_order.order_id');
        $this->db->where('sales_order.order_state', $order_state); //订单当前状态
        $this->db->where('sales_order.order_status', $order_status); //订单当前状态
        $this->db->where('line_order_product.start_date <=', date('Y-m-d H:i:s')); //价格日期一定要小于当前日期
        return $this->db->get();
    }
   /**
    * 找出分类订单的总结
    *  @param unknown $uid
    */
    public function findOrderByStatus($uid){
    	$this->db->select('count(*) as total,sales_order.order_status');
    	$this->db->from($this->table);
    	$this->db->join($this->table_2.' as line_order_product', 'line_order_product.order_id = sales_order.order_id');
    	$this->db->where('uid',$uid);
    	$this->db->group_by('sales_order.order_status');
    	return $this->db->get();
    }
     
    /**
     * 散客和分销的订单总数
     * @param unknown $getData
     */
    public function selectSalesOrderTotal($getData)
    {
        $this->db->select('sales_order.*, line_order_product.line_id, line_order_product.line_name, line_order_product.start_date, line_order_product.travel_days, line_order_product.adult_num, line_order_product.children_num, line_order_product.children_num,pay_style.pay_name');
        $this->db->from($this->table.' as sales_order');
        $this->db->join($this->table_2.' as line_order_product', 'line_order_product.order_id = sales_order.order_id');
        $this->db->join('pay_style','pay_style.id = sales_order.pay_style_type');
        $this->db->where('sales_order.order_status', $getData['order_status']); //订单当前状态
        $this->db->where('sales_order.uid', $getData['uid']); //当前登录用户uid
        if (!empty($getData['line_name'])) {
            $this->db->like('line_order_product.line_name', $getData['line_name']);
        }
        if (!empty($getData['order_sn'])) {
            $this->db->where('sales_order.order_sn', $getData['order_sn']);
        }
        return $this->db->count_all_results();
    }
    
    /**
     * 散客和分销的订单列表
     * @param unknown $num
     * @param unknown $getData
     */
    public function selectSalesOrderList($page_num, $num, $getData)
    {
        $this->db->select('sales_order.*, line_order_product.line_id, line_order_product.line_name, line_order_product.start_date, line_order_product.travel_days, line_order_product.adult_num, line_order_product.children_num, line_order_product.children_num,pay_style.pay_name');
        $this->db->from($this->table.' as sales_order');
        $this->db->join($this->table_2.' as line_order_product', 'line_order_product.order_id = sales_order.order_id');
        $this->db->join('pay_style','pay_style.id = sales_order.pay_style_type');
        $this->db->where('sales_order.order_status', $getData['order_status']); //订单当前状态
        $this->db->where('sales_order.uid', $getData['uid']); //当前登录用户uid
        if (!empty($getData['line_name'])) {
            $this->db->like('line_order_product.line_name', $getData['line_name']);
        }
        if (!empty($getData['order_sn'])) {
            $this->db->where('sales_order.order_sn', $getData['order_sn']);
        }
        $this->db->order_by('sales_order.order_id','desc');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
    /**
     * 判断是否已经为已经付款完成订单,如果
     * @param unknown $order_id
     * @param unknown $order_state
     * @param unknown $order_status
     */
    public function findByStatus($order_id,$order_state,$order_status){
    	
        $this->db->select('sales_order.*');
        $this->db->from($this->table.' as sales_order');
        $this->db->where('sales_order.order_state',$order_state);
        $this->db->where('sales_order.order_status',$order_status);
        $this->db->where('sales_order.order_id',$order_id);
        return $this->db->get();
    }
}